/********************************************************/
/*	Copyright (C) 2016 Gong Li Bin		 	*/
/*	Project:	GlbRtbEng-1.0.0			*/
/*	Author:		gong_libin			*/
/*	Date:		2016_06_26			*/
/*	File:		GlbRtbAdapter.cpp		*/
/********************************************************/

#include "GlbPro.h"
#include "GlbUdp.h"
#include "GlbTime.h"
#include "GlbRtbDefs.h"

int main(int argc, char* argv[])
{
	int iLength = 0;
	GlbNet::CGlbUdp CUdp;
	GlbNet::CGlbPro CSend;
	GlbNet::CGlbPro CRecv;
	GlbCls::CGlbTime CTime;
	UCHAR uszRecv[GLB_KBYTES8] = { 0 };
	UCHAR uszSend[GLB_KBYTES8] = { 0 };
	char szUrl[GLB_URL] = "127.0.0.1:20162";
	GlbNet::GLBPROKV_S stKv[] = {
		{GLB_RD_PBID, strlen("2203b0295c15ead4a9bb106968bfbc"), (UCHAR*)"2203b0295c15ead4a9bb106968bfbc"},
		{GLB_RD_ADDR, strlen("101.68.83.205"), (UCHAR*)"101.68.83.205"},
		{GLB_RD_UAGT, strlen("Mozilla/4.0 (compatible; Windows NT 5.1; SV1;)"), (UCHAR*)"Mozilla/4.0 (compatible; Windows NT 5.1; SV1;)"},
		{GLB_RD_UURL, strlen("http://news.cn.yahoo.com/newspic/news/20475/20/"), (UCHAR*)"http://news.cn.yahoo.com/newspic/news/20475/20/"},
		{GLB_RD_PPID, strlen("mm_14539978_2071324_8355258"), (UCHAR*)"mm_14539978_2071324_8355258"},
		{GLB_RD_SIZE, strlen("300x250"), (UCHAR*)"300x250"},
	};

	CSend.GlbProPutBody(NULL);
	CSend.GlbProPutVerb(GLB_RD_REQ);
	CSend.GlbProPutType(GLB_RD_TANX);
	CSend.GlbProPutTotal(sizeof(stKv) / sizeof(GlbNet::GLBPROKV_S));
	CSend.GlbProPutLength(GlbProKvOutput(stKv, CSend.GlbProGetTotal(), uszSend + GLB_PRO_HEAD, GLB_KBYTES8));
	CSend.GlbProCreate(uszSend, GLB_PRO_HEAD);

	CTime.GlbTimeOn();

	if (GLB_SUCCESS == CUdp.GlbNetInitSend(szUrl)) {
		CUdp.GlbNetSetTimeOut(GLB_TIMEOUT, 0);
		if (CUdp.GlbNetSend(uszSend, GLB_PRO_HEAD + CSend.GlbProGetLength()) > 0) {
			if ((iLength = CUdp.GlbNetRecv(uszRecv, GLB_KBYTES8)) > 0) {
				/* head */
				CRecv.GlbProParse(uszRecv, iLength);
				CRecv.GlbProDisplay();
				/* body */
				if (GLB_DEF_SUCC == CRecv.GlbProGetResult()) {
					memset(stKv, '\0', sizeof(GlbNet::GLBPROKV_S) * (sizeof(stKv) / sizeof(GlbNet::GLBPROKV_S)));
					GlbProKvInput(stKv, CRecv.GlbProGetTotal(), CRecv.GlbProGetBody(), CRecv.GlbProGetLength());
					GlbProKvDisplay(stKv, CRecv.GlbProGetTotal());
				}
			}
			else {
				GLB_ERROR("Failed to GlbNetRecv\n");
			}
		}
		else {
			GLB_ERROR("Failed to GlbNetSend\n");
		}
		CUdp.GlbNetClose();
	}
	else {
		GLB_ERROR("Failed to GlbNetInitSend\n");
	}

	CTime.GlbTimeOff();
	CTime.GlbTimeDisplay();

	return GLB_SUCCESS;
}
